<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="google-site-verification" content="yaeorbXgKPwp5BDegBtAJEHYWFEFE-BViWTj74gJPrw" />
<!--  -->
<link href="/css/default.css" type="text/css" rel="stylesheet" />
<!--  -->
<script type="text/javascript" src="/js/jquery.js"></script>
<script type="text/javascript" src="/lang/en.js"></script>
<script type="text/javascript" src="/js/mac/core.min.js"></script>
<title>mac-rpc - 示例</title>
<meta name="description" content="RPC 远程方法调用" />
<meta name="keywords" content="Javascript JQuery Ext Grid ComoBox Tree Tabs Plugin Demo Download" />
<script type="text/javascript">
$(function(){
	function adjust(){
		var de = document.documentElement
			, ch = de.clientHeight, cw = de.clientWidth;
		$('#body').width(cw - 16);
	}
	$(window).resize(adjust);
	adjust();
});
</script>
</head>
<body>
<div id="head">
	<a href="/en/"><img src="/css/images/logo_s.png" alt="logo" /></a>
	<div class="title">BOARSOFT.COM</div>
	<div class="right">
		<div>
			<a href="/public/lang.php?l=en">English</a>
			|
			<a href="/public/lang.php?l=cn">简体中文</a>
			|
			<a href="http://activemq.apache.org/" target="_blank">ActiveMQ</a>
			|
			<a href="https://redis.io/" target="_blank">Redis</a>
			|
			<a href="http://www.jquery.com/" target="_blank">JQuery</a>
		</div>
	</div>
</div><div id="body">
	<div class="dir">
<h1>目录</h1>
<hr/>
<ul>
	<li><a href="./index.html">基本概念</a></li>
	<ul>
		<li><a href="./index.html#node">服务消费者与提供者</a></li>
		<li><a href="./index.html#svc">服务四要素</a></li>
		<li><a href="./index.html#reg">服务注册与发现</a></li>
		<li><a href="./index.html#call">服务调用过程</a></li>
	</ul>
	<li><a href="./config.html">配置说明</a></li>
	<ul>
		<li><a href="./config.html#basic">基本配置</a></li>
		<li><a href="./config.html#registry">注册中心配置</a></li>
		<li><a href="./config.html#provider">服务提供者配置</a></li>
		<li><a href="./config.html#consume">服务消费者配置</a></li>
	</ul>
	<li><a href="./demo.html#">用法示例</a><li>
	<ul>
		<li><a href="./demo.html">同步调用</a></li>
		<li><a href="./demo.html#sc">指定服务提供者</a></li>
		<li><a href="./demo.html#ac">异步调用</a></li>
		<li><a href="./demo.html#sb">同步广播方法</a></li>
		<li><a href="./demo.html#ab">异步广播方法</a></li>
	</ul>
	<li><a href="./console.html">RPC管控</a><li>
	<ul>
		<li><a href="./console.html#node">节点视图</a></li>
		<li><a href="./console.html#svc">服务视图</a></li>
		<li><a href="./console.html#status"></a>开关</li>
		<li><a href="./console.html#limit"></a>限流</li>
		<li><a href="./console.html#toggle"></a>升降级</li>
	</ul>
	<li><a href="./advance.html">进阶应用</a><li>
	<ul>
		<li><a href="./advance.html#seriallize">自定义序列化器</a></li>
		<li><a href="./advance.html#protocal">自定义传输协议</a></li>
		<li><a href="./advance.html#threadpool">自定义线程池</a></li>
		<li><a href="./advance.html#tune">性能调优</a></li>
		<li><a href="./advance.html#more">更多可能</a></li>
	</ul>
	<li><a href="./qa.html">常见问题</a></li>
</ul>	</div>
	<div class="main">
		<div id="nav">
			<div id="nav_path">
				<a href="/en/">Home</a>
				&gt;
				<a href="/en/mac/rpc">mac-rpc</a>
				&gt;
				示例
			</div>
			<div id="nav_back">
				<a href="javascript:history.back()">Back</a>
				|
				<a href="/en/">UP</a> &nbsp;&nbsp;
			</div>
			<div class="clear"></div>
		</div>
<h1>mac-rpc - 示例</h1>
前往下载&nbsp;<a href="/en/download.html#rpcDemo">RPC应用示例</a>
|
<a href="/en/download.html#jar">依赖JAR包与源码下载</a>
<h2>同步调用</h2>
&nbsp;&nbsp;服务提供者：provide.xml<br/>
<xmp>
	<service ref="demoService" group="demo" name="demo1"
		interface="com.boarsoft.rpc.sample.DemoService" version="1.0.0"
		timeout="3000">
	</service>
</xmp>
&nbsp;&nbsp;服务提供者：DemoServiceImpl.java<br/>
<xmp>
	@Component("demoService")
	public class DemoServiceImpl implements DemoService {
		@Override
		public String helloSC(User u) {
			if (u == null || Util.strIsEmpty(u.getName())) {
				throw new IllegalArgumentException("Invalid parameter");
			}
			return "Hello ".concat(u.getName());
		}
	}
</xmp>
&nbsp;&nbsp;服务消费者：consume.xml<br/>
<xmp>
	<reference id="demoService" group="demo" name="demo1"
		interface="com.boarsoft.rpc.sample.DemoService" version="1.0.0"
		timeout="6000">
	</reference>
</xmp>
&nbsp;&nbsp;服务消费者：Main.java<br/>
<xmp>
	// 从Spring容器中获取，也可以通过@Autowired等注解方式注入
	DemoService ds = (DemoService) ctx.getBean("demoService");
	Object rs = null;
	
	ro = ds.helloSC(new User("Mac_J"));
	log.info("Result = {}", ro);

	try {
		// 此句会抛出来自服务提供者的异常
		ro = ds.helloSC(new User());
		log.info("Result = {}", ro);
	} catch (Exception e) {
		log.error("AC failed", e);
	}
</xmp>
<h2><a name="sc"></a>指定服务提供者</h2>
&nbsp;&nbsp;RpcContext.specify2用于指定服务提供者<br/>
<xmp>
	RpcContext.specify2("127.0.0.1:9101");
	try {
		ro = ds.helloSC(new User("Mac_J"));
		log.info("Result = {}", ro);
	} finally {
		RpcContext.specify2(null);
	}
</xmp>
<h2><a name="ac"></a>异步调用</h2>
&nbsp;&nbsp;异步调用方法的返回类型必须声明为Object，在编写实现时，需要返回真实的结果。但调用方在调用时获得的是一个Future&lt;Object&gt;<br/>
&nbsp;&nbsp;服务消费者：consume.xml<br/>
<xmp>
	<reference id="demoService" group="demo" name="demo1"
		interface="com.boarsoft.rpc.sample.DemoService" version="1.0.0"
		timeout="6000">
		<method name="helloAC" type="AC">
			<arg type="com.boarsoft.rpc.sample.User" />
		</method>
	</reference>
</xmp>
&nbsp;&nbsp;服务消费者：Main.java<br/>
<xmp>
	Future<Object> ft = null;
	ft = (Future<Object>) ds.helloAC(new User("Mac_J"));
	log.info("Do something before get actual result");
	ro = ft.get();
	log.info("Result = {}", ro);

	// 此句不会抛出异常，返回的结果是远程方法内部抛出的异常
	ft = (Future<Object>) ds.helloAC(new User());
	log.info("Do something before get actual result");
	ro = ft.get();
	log.info("Result = {}", ro);
</xmp>
<h2><a name="sb"></a>同步广播方法</h2>
&nbsp;&nbsp;同步广播方法也是是需要特殊处理的方法，其返回值必须是Object类型。<br/>
&nbsp;&nbsp;在编写服务方的实现时，需要返回真实的结果。但在服务的调用方（服务消费者）拿到的对象却时Map&lt;String, Object&gt;类型。此map的key是执行此方法的节点的地址，形如：IP:PORT，而map的value才是实际的结果。
&nbsp;&nbsp;服务消费者：consume.xml<br/>
<xmp>
	<reference id="demoService" group="demo" name="demo1"
		interface="com.boarsoft.rpc.sample.DemoService" version="1.0.0"
		timeout="6000">
		<method name="helloSB" type="SB">
			<arg type="com.boarsoft.rpc.sample.User" />
		</method>
	</reference>
</xmp>
&nbsp;&nbsp;服务消费者：Main.java<br/>
<xmp>
	Map<String, Object> rm = null;
	rm = (Map<String, Object>) ds.helloSB(new User("Mac_J"));
	for (String k : rm.keySet()) {
		ro = rm.get(k);
		log.info("{} = {}", k, ro);
	}

	rm = (Map<String, Object>) ds.helloSB(new User());
	for (String k : rm.keySet()) {
		ro = rm.get(k);
		log.info("{} = {}", k, ro);
	}
</xmp>
<h2><a name="ab"></a>异步广播方法</h2>
&nbsp;&nbsp;异步广播方法与同步广播方法在处理上基本相同。异步广播方法采用Future模式，返回Map&lt;String, Future&lt;Object&gt;&gt;，因此在获取实际结果前不会阻塞当前线程。
&nbsp;&nbsp;服务消费者：consume.xml<br/>
<xmp>
	<reference id="demoService" group="demo" name="demo1"
		interface="com.boarsoft.rpc.sample.DemoService" version="1.0.0"
		timeout="6000">
		<method name="helloAB" type="AB">
			<arg type="com.boarsoft.rpc.sample.User" />
		</method>
	</reference>
</xmp>
&nbsp;&nbsp;服务消费者：Main.java<br/>
<xmp>
	Map<String, Object> rm = null;
	rm = (Map<String, Object>) ds.helloAB(new User("Mac_J"));
	for (String k : rm.keySet()) {
		ro = rm.get(k);
		if (ro != null && ro instanceof Future) {
			ro = ((Future<Object>) ro).get();
		}
		log.info("{} = {}", k, ro);
	}

	rm = (Map<String, Object>) ds.helloAB(new User());
	for (String k : rm.keySet()) {
		ro = rm.get(k);
		if (ro != null && ro instanceof Future) {
			ro = ((Future<Object>) ro).get();
		}
		log.info("{} = {}", k, ro);
	}
</xmp>
	</div>
</div>
<div id="foot">
Copyright © 2011. All rights reserved&nbsp;&nbsp;蜀ICP备11014774号-1&nbsp;&nbsp;
QQ: 7213571&nbsp;&nbsp;E-mail: pyh_jerry@163.com&nbsp;&nbsp;
<!-- 
<script type="text/javascript">
var _bdhmProtocol = (("https:" == document.location.protocol) ? " https://" : " http://");
document.write(unescape("%3Cscript src='" + _bdhmProtocol + "hm.baidu.com/h.js%3Ffc0b0282216be3d16055855d8c0e72d9' type='text/javascript'%3E%3C/script%3E"));
</script>
&nbsp;&nbsp;
 -->
</div></body>
</html>
